package com.liugui.thread;

/**
 * @ClassName: ThreadLocalDemo
 * @Author: liugui
 * @Date: 2020-11-09 10:00
 **/
public class ThreadLocalDemo {
    public static void main(String[] args) {
        ThreadDemo threadDemo = new ThreadDemo();
        new Thread(threadDemo).start();
        new Thread(threadDemo).start();
    }
}

class ThreadDemo implements Runnable {
    //共享变量
    private Integer integer = 0;
    //使用ThreadLocal提供的静态方法创建一个线程变量 并且初始化值为0
    private static ThreadLocal<Integer> threadLocal = ThreadLocal.withInitial(() -> 0);

    @Override
    public void run() {
        for (int i = 0; i < 10; i++) {
            //get方法获取线程变量值
//            Integer integer = threadLocal.get();
//            integer += 1;
//            //set方法设置线程变量值
//            threadLocal.set(integer);
//            System.out.println(integer);

            //线程不安全
            integer++;
            System.out.println(integer);
        }
    }
}